package cqut.gbq.qimo.controller;

import cqut.gbq.qimo.entity.Course;
import cqut.gbq.qimo.entity.ResBean;
import cqut.gbq.qimo.service.CourseService;
import cqut.gbq.qimo.utils.GenerateCourseCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cqut.gbq.qimo.entity.Assignment;
import cqut.gbq.qimo.entity.Course;
import cqut.gbq.qimo.entity.ResBean;
import cqut.gbq.qimo.service.AssignmentService;
import cqut.gbq.qimo.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @ClassName CourseController
 * @Description TODO
 * @Author guobenqi
 * @Date 2021/6/18 14:41
 */
@Api(tags = "课程相关接口")
@RestController
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private AssignmentService assignmentService;

    @ApiOperation("创建课程")
    @PostMapping("/createCourse")
    private ResBean getAllStudents(@RequestBody Course course){
        System.out.println(course);
        String code = GenerateCourseCode.getCharAndNumr();
        course.setCourseCode(code);
        return courseService.createCourse(course);
    }

    @ApiOperation("获得对应老师的课程信息")
    @GetMapping("/getClassInfoByTid")
    private ResBean getClassInfo(@RequestParam Integer tid){
        System.out.println(tid);

        List<Course> courseList =  courseService.getClassInfoByTid(tid);

        courseList.forEach(item->{
            List<Assignment> assignments = assignmentService.findAssignmentsByCid(item.getCid());
            item.setAssignmentList(assignments);
        });

        return  ResBean.success("获取成功",courseList);
    }

    @ApiOperation("删除课程")
    @DeleteMapping("/deleteCourseByCid")
    private ResBean deleteCourse(@RequestParam Integer tid,@RequestParam Integer cid){
        boolean flag = courseService.deleteCourseByCid(tid,cid);

        if (flag){
            return ResBean.success("删除成功");
        } else {
            return ResBean.error("删除失败");
        }
    }

    @ApiOperation("查询课程的对应作业")
    @GetMapping("/getAssignmentLinkCourse")
    private ResBean getAssignmentLinkCourse(@RequestParam Integer cid,@RequestParam Integer sid){
        List<Assignment> assignmentList = courseService.getAssignmentLinkCourse(cid);
        if (assignmentList != null){
            assignmentList.forEach(item->{
                Integer notSubmit = courseService.getNotSubmit(item.getAid());
                Integer notCheck = courseService.getNotCheck(item.getAid());
                Integer check = courseService.getCheck(item.getAid());
                item.setNoHandinCount(notSubmit);
                item.setNoCheckCount(notCheck);
                item.setIsCheckCount(check);
                if (sid != -1){
                    Integer isHandin = courseService.getAssIsHandIn(item.getAid(),sid);
                    item.setIsHandin(isHandin);
                    Integer score = courseService.getScore(item.getAid(),sid);
                    item.setScore(score);
                    Integer isCheck = courseService.getIscheck(item.getAid(),sid);
                    item.setIsCheck(isCheck);
                }

            });
            return ResBean.success("查询成功",assignmentList);
        } else {
            return ResBean.error("查询失败");
        }
    }
    @ApiOperation("查询课程的对应人数")
    @GetMapping("/getPeopleNumberOfCourse")
    private ResBean getPeopleNumberOfCourse(@RequestParam Integer course_id){
        Integer number = courseService.getPeopleNumber(course_id);
        if (number != null){
            return ResBean.success("查询成功",number);
        } else {
            return ResBean.error("查询失败");
        }
    }

    @ApiOperation("修改课程信息")
    @PostMapping("/editCourseInfo")
    public ResBean editCourseInfo(@RequestBody Course course){

        return courseService.editCourseInfo(course);

    }

    @ApiOperation("将课程置顶")
    @GetMapping("/updateTop")
    public ResBean updateTop(@RequestParam Integer cid){
        Integer row = courseService.updateTop(cid);
        if (row == 1){
            return ResBean.success("置顶成功");
        } else {
            return ResBean.error("置顶失败");
        }
    }

    @ApiOperation("将课程取消置顶")
    @GetMapping("/updateNoTop")
    public ResBean updateNoTop(@RequestParam Integer cid){
        Integer row = courseService.updateNoTop(cid);
        if (row == 1){
            return ResBean.success("取消置顶成功");
        } else {
            return ResBean.error("取消置顶失败");
        }
    }

    @ApiOperation("查询置顶课程")
    @GetMapping("/getCourseTop")
    public ResBean getCourseTop(){
        List<Course> courseTop = courseService.getCourseTop();
        if (courseTop!=null){
            courseTop.forEach(item-> {
                List<Assignment> assignments = courseService.getAssignmentLinkCourse(item.getCid());
                item.setAssignmentList(assignments);
                System.out.println("");
            });
            return ResBean.success("查询置顶课程成功", courseTop);
        } else {
            return ResBean.error("查询置顶课程失败");
        }
    }




}
